﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Main_Inventory_SaleReturnItems : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strsql = "";
    string previousDate = "";
    string previousBill = "";
    int firstRow = 0;
    int firstRow1 = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            BindDataList();
            strsql = "select LNAME from acc_ledmast where LID='" + Request.QueryString["Lid"].ToString() + "'";
            DataTable DT = DU.getdata(strsql);
            if (DT.Rows.Count > 0)
            {
                lblCustomer.Text = DT.Rows[0]["LNAME"].ToString();
            } 
        }        
    }
    protected void BindDataList()
    {
        string strDate = string.Format("{0:MM/dd/yyyy}", System.DateTime.Now.ToString());
        if(Request.QueryString["CurDate"]!=null)
        {
            string[] str = Request.QueryString["CurDate"].ToString().Split('/');
            strDate = str[1] + "/" + str[0] + "/" + str[2];
        }
        strsql = "select ivr_tran_main.trnid,ivr_tran_main.tfoid,ivr_tranformat.formatname from ivr_tran_main inner join ivr_tranformat on ivr_tran_main.tfoid=ivr_tranformat.tfoid where ivr_tran_main.vendcust_lid='" + Request.QueryString["Lid"].ToString() + "' and ivr_tran_main.trantypeid=1 and ivr_tran_main.trndate<='" + strDate + "'";
        DataTable DT = DU.getdata(strsql);
        datalistMain.DataSource = DT;
        datalistMain.DataBind();
    }
    protected void datalistMain_ItemBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HiddenField hidTranId = (HiddenField)e.Item.FindControl("hidTranId");
            GridView grdDisplay = (GridView)e.Item.FindControl("grdDisplay");
            SqlParameter[] sparam ={
                                       new SqlParameter("@TRNID",hidTranId.Value.ToString()),
                                       new SqlParameter("@FINYEAR",Session["FINYEAR"].ToString())
                                   };
            DataTable DT = DU.getdatausingprocedure("USP_IVR_GetItemDetalisForSalesPurchaseReturn", sparam);
            if (DT.Rows.Count > 0)
            {
                grdDisplay.DataSource = DT;
                grdDisplay.DataBind();
            }
        }
        //if (e.Item.ItemType == ListItemType.Header)
        //{
        //    Label lblCustomer = (Label)e.Item.FindControl("lblCustomer");
        //    strsql = "select LNAME from acc_ledmast where LID='" + Request.QueryString["Lid"].ToString() + "'";
        //    DataTable DT = DU.getdata(strsql);
        //    if (DT.Rows.Count > 0)
        //    {
        //        lblCustomer.Text = DT.Rows[0]["LNAME"].ToString();
        //    }            
        //}
    }
    protected void grdDisplay_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridView grdDisplay = (GridView)sender;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkItem = (CheckBox)e.Row.Cells[2].FindControl("chkItem");
            HiddenField hidItemId = (HiddenField)e.Row.Cells[2].FindControl("hidItemId");
            chkItem.Attributes.Add("onclick", "return SelectAll(this.id,'" + hidItemId.ClientID + "');");
            HiddenField hidTrnItemId = (HiddenField)e.Row.Cells[2].FindControl("hidTrnItemId");//onclick="return SelectAll(this.id);"
            if (Session["DTSaleReturn"] != null)
            {
                DataTable DTCheck = (DataTable)Session["DTSaleReturn"];
                if (DTCheck.Rows.Count > 0)
                {
                    for (int i = 0; i < DTCheck.Rows.Count; i++)
                    {
                        if (hidTrnItemId.Value.ToString() == DTCheck.Rows[i]["TITID"].ToString())
                        {                            
                            chkItem.Checked = true;
                        }                       
                    }
                }
            }
        }        
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        GetCheckedbox();
        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "DisplayData();", true);            
    }
    protected void GetCheckedbox()
    {
        DataTable DTMain = new DataTable();
        DTMain.Columns.Add("TITID");
        DTMain.Columns.Add("ItemId");
        DTMain.Columns.Add("ItemCode");
        DTMain.Columns.Add("ItemName");
        DTMain.Columns.Add("UOMID");
        DTMain.Columns.Add("UomSymbol");
        DTMain.Columns.Add("Rate");
        DTMain.Columns.Add("Qty");
        for (int i = 0; i < datalistMain.Items.Count; i++)
        {
            GridView grdDisplay = (GridView)datalistMain.Items[i].FindControl("grdDisplay");
            for (int j = 0; j < grdDisplay.Rows.Count; j++)
            {
                CheckBox chkItem = (CheckBox)grdDisplay.Rows[j].Cells[2].FindControl("chkItem");
                HiddenField hidItemId = (HiddenField)grdDisplay.Rows[j].Cells[2].FindControl("hidItemId");
                HiddenField hidUomId = (HiddenField)grdDisplay.Rows[j].Cells[2].FindControl("hidUomId");
                HiddenField hidUomSymbol = (HiddenField)grdDisplay.Rows[j].Cells[2].FindControl("hidUomSymbol");
                HiddenField hidTrnItemId = (HiddenField)grdDisplay.Rows[j].Cells[2].FindControl("hidTrnItemId");
                if (chkItem.Checked == true)
                {
                    DataRow DRMain = DTMain.NewRow();
                    DRMain["TITID"] = hidTrnItemId.Value.ToString();
                    DRMain["ItemId"] = hidItemId.Value.ToString();
                    DRMain["ItemCode"] = grdDisplay.Rows[j].Cells[3].Text;
                    DRMain["ItemName"] = grdDisplay.Rows[j].Cells[4].Text;
                    DRMain["UOMID"] = hidUomId.Value.ToString();
                    DRMain["UomSymbol"] = hidUomSymbol.Value.ToString();
                    DRMain["Rate"] = grdDisplay.Rows[j].Cells[5].Text;
                    DRMain["Qty"] = grdDisplay.Rows[j].Cells[6].Text;
                    DTMain.Rows.Add(DRMain);
                }
            }
        }
        Session["DTSaleReturn"] = DTMain;
    }
    public static void MergeRows(GridView gridView)
    {
        for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
        {
            GridViewRow row = gridView.Rows[rowIndex];
            GridViewRow previousRow = gridView.Rows[rowIndex + 1];

            for (int i = 0; i < 2; i++)
            {
                if (row.Cells[i].Text == previousRow.Cells[i].Text)
                {
                    row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 : previousRow.Cells[i].RowSpan + 1;
                    previousRow.Cells[i].Visible = false;
                }
            }
        }
    }
    protected void grdDisplay_PreRender(object sender, EventArgs e)
    {
        for (int i = 0; i < datalistMain.Items.Count; i++)
        {
            GridView grdDisplay = (GridView)datalistMain.Items[i].FindControl("grdDisplay");
            MergeRows(grdDisplay);
        }
    }
    protected void BindDataListBasedOnTwoDate()
    {
        string strFromDate = string.Empty;
        string strToDate = string.Empty;
        if (txtDateFrom.Text != string.Empty)
        {
            string[] str = txtDateFrom.Text.ToString().Split('/');
            strFromDate = str[1] + "/" + str[0] + "/" + str[2];
        }
        if (txtDateTo.Text != string.Empty)
        {
            string[] str = txtDateTo.Text.ToString().Split('/');
            strToDate = str[1] + "/" + str[0] + "/" + str[2];
        }
        strsql = "select ivr_tran_main.trnid,ivr_tran_main.tfoid,ivr_tranformat.formatname from ivr_tran_main inner join ivr_tranformat on ivr_tran_main.tfoid=ivr_tranformat.tfoid where ivr_tran_main.vendcust_lid='" + Request.QueryString["Lid"].ToString() + "' and ivr_tran_main.trantypeid=1 and ivr_tran_main.trndate between '" + strFromDate + "' and '" + strToDate + "'";
        DataTable DT = DU.getdata(strsql);
        datalistMain.DataSource = DT;
        datalistMain.DataBind();
    }
    protected void btnShow_Click(object sender, EventArgs e)
    {
        BindDataListBasedOnTwoDate();
    }
}




    
    


